<?php
class PhormParams
{
	private $applicationName;
	private $mediaPath;
	private $databaseType;
	private $databaseUsername;
	private $databasePassword;
	private $databaseHost;
	private $databaseName;
	private $databaseEngine;
	private $charset;
	private $dateFormat;
	private $pathToLib;
	private $sessionHandler;

	public function __construct()
	{
		$this->applicationName  = 'phORM';
		$this->mediaPath        = 'managed/media';
		$this->databaseType     = 'mysql';
		$this->databaseUsername = 'root';
		$this->databasePassword = '';
		$this->databaseHost     = 'localhost';
		$this->databaseName     = 'phorm';
		$this->databaseEngine   = 'InnoDB';
		$this->charset          = 'latin1';
		$this->dateFormat       = 'Y-m-d';        
		$this->pathToLib        = $_SERVER['DOCUMENT_ROOT'].'/phorm';
		$this->sessionHandler   = 'PhormSession';
	}

	public function __get($name)
	{
		if(property_exists($this, $name))
			return $this->$name;
	}

	public function __set($name, $value)
	{
		if(property_exists($this, $name))
			$this->$name = $value;
	}
}

class PhormConfig
{
	// Hold an instance of the class
	private static $instance;

	private $PhormParams;

	// A private constructor; prevents direct creation of object
	private function __construct()
	{
		$this->PhormParams  = new PhormParams();
	}

	// The singleton method
	public static function getConfig()
	{
		if (!isset(self::$instance))
		{
			$class = __CLASS__;
			self::$instance = new $class;
		}

		return self::$instance;
	}
	
	public static function getSession()
	{
		$session = null;
		
		if(isset($_SESSION['PhormSession']))
		{
			$session = $_SESSION['PhormSession'];
		}
		else
		{
			$class   = self::getConfig()->sessionHandler;
			$session = new $class();
		}
		
		return $session;
	}

	public static function setConfig(PhormParams $PhormParams)
	{
		if (!isset(self::$instance))
			self::getConfig();
			
		self::$instance->PhormParams = $PhormParams;
	}

	public function __get($name)
	{
		return self::$instance->PhormParams->$name;
	}

	public function __clone()
	{
		trigger_error('Clone is not allowed.', E_USER_ERROR);
	}
}

?>
